package cn.jsl.pet.controller;

import cn.jsl.basic.exception.MyException;
import cn.jsl.basic.util.AjaxResult;
import cn.jsl.basic.util.PageList;
import cn.jsl.pet.domain.SearchMasterMsg;
import cn.jsl.pet.dto.MsgDto;
import cn.jsl.pet.query.SearchMasterMsgQuery;
import cn.jsl.pet.service.ISearchMasterMsgService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
                searchMasterMsgService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryPage(query);
    }

    /**
    * 批量删除, (restful风格接口中,批量相关的都要用这个注解: @PatchMapping("/{id}"))
    * @param ids
    * @return
    */
    @PatchMapping // 这里地址不带值
    //Delete http://localhost:8080/department/1
    @ApiOperation(value = "批量删除数据" )
    public AjaxResult batchRemove(@RequestBody List<Long> ids){ // 前端传过来的List<ids>
        // 包起来,快捷键: ctr + alt + T
        try {
            searchMasterMsgService.patchDelete(ids); // ids是前端传过来的ids数组,包含了批量删除的部门对象的id.
            return AjaxResult.me(); // 默认成功信息
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("系统繁忙，稍后重试.."+e.getMessage())
                    .setSuccess(false); // 链式编程
        }
    }

    @PostMapping("/publish")
    @ApiOperation(value = "发布寻主消息" )
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){ // 前端传过来的List<ids>
        // 包起来,快捷键: ctr + alt + T
        try {
            searchMasterMsgService.publish(searchMasterMsg,request);
            return AjaxResult.me(); // 默认成功信息
        } catch (MyException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("系统繁忙，稍后重试.."+e.getMessage())
                    .setSuccess(false); // 链式编程
        }
    }

    //用户
    @PostMapping("/user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        return searchMasterMsgService.user(query,request);
    }

    //查询待审核驳回
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query) {
        query.setState(0);
        return searchMasterMsgService.queryPage(query);
    }

    //已完成
    @PostMapping("/over")
    public PageList<SearchMasterMsg> over(@RequestBody SearchMasterMsgQuery query) {
        query.setState(2);
        return searchMasterMsgService.queryPage(query);
    }

    //寻主池
    /*@PostMapping("/searchMasterPool")
    public PageList<SearchMasterMsg> searchMasterPool(@RequestBody SearchMasterMsgQuery query) {
        query.setState(1);
        return searchMasterMsgService.queryPage(query);
    }*/

    //审核成功待处理
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request) {
        query.setState(1);
        return searchMasterMsgService.toHandle(query,request);
    }

    //拒单
    @GetMapping("/reject/{msgId}")
    public AjaxResult reject(@PathVariable("msgId") Long msgId) {
        try {
            searchMasterMsgService.reject(msgId);
            return AjaxResult.me(); // 默认成功信息
        } catch (MyException e) {
            e.printStackTrace();
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setMessage("系统繁忙，稍后重试.."+e.getMessage())
                    .setSuccess(false); // 链式编程
        }
    }

    //接单accept
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody MsgDto msgDto) {
        try {
            searchMasterMsgService.accept(msgDto);
            return new AjaxResult();
        }  catch (MyException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统错误[500]");
        }
    }
}
